// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "routablefactories50.h"

namespace document { class DocumentTypeRepo; }

namespace documentapi {
/**
 * This class encapsulates all the {@link RoutableFactory} classes needed to implement factories for the document
 * routable. When adding new factories to this class, please KEEP THE THEM ORDERED alphabetically like they are now.
 */
class RoutableFactories51 {
public:
    RoutableFactories51() = delete;

    /**
     * Implements the shared factory logic required for {@link DocumentMessage} objects, and it offers a more
     * convenient interface for implementing {@link RoutableFactory}.
     */
    class DocumentMessageFactory : public IRoutableFactory {
    protected:
        /**
         * This method encodes the given message into the given byte buffer. You are guaranteed to only receive messages of
         * the type that this factory was registered for.
         *
         * This method is NOT exception safe. Return false to signal failure.
         *
         * @param msg The message to encode.
         * @param buf The byte buffer to write to.
         * @return True if the message was encoded.
         */
        virtual bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const = 0;

        /**
         * This method decodes a message from the given byte buffer. You are guaranteed to only receive byte buffers
         * generated by a previous call to {@link #doEncode(DocumentMessage, GrowableByteBuffer)}.
         *
         * This method is NOT exception safe. Return null to signal failure.
         *
         * @param buf The byte buffer to read from.
         * @return The decoded message.
         */
        virtual DocumentMessage::UP doDecode(document::ByteBuffer &buf) const = 0;

    public:
        /**
         * Convenience typedefs.
         */
        typedef std::unique_ptr<IRoutableFactory> UP;
        typedef std::shared_ptr<IRoutableFactory> SP;

        bool encode(const mbus::Routable &obj, vespalib::GrowableByteBuffer &out) const override;
        mbus::Routable::UP decode(document::ByteBuffer &in, const LoadTypeSet& loadTypes) const override;
    };

    /**
     * Implements the shared factory logic required for {@link DocumentReply} objects, and it offers a more
     * convenient interface for implementing {@link RoutableFactory}.
     */
    class DocumentReplyFactory : public IRoutableFactory {
    protected:
        /**
         * This method encodes the given reply into the given byte buffer. You are guaranteed to only receive
         * replies of the type that this factory was registered for.
         *
         * This method is NOT exception safe. Return false to signal failure.
         *
         * @param reply The reply to encode.
         * @param buf The byte buffer to write to.
         * @return True if the message was encoded.
         */
        virtual bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const = 0;

        /**
         * This method decodes a reply from the given byte buffer. You are guaranteed to only receive byte buffers
         * generated by a previous call to {@link #doEncode(DocumentReply, GrowableByteBuffer)}.
         *
         * This method is NOT exception safe. Return null to signal failure.
         *
         * @param buf The byte buffer to read from.
         * @return The decoded reply.
         */
        virtual DocumentReply::UP doDecode(document::ByteBuffer &buf) const = 0;

    public:
        /**
         * Convenience typedefs.
         */
        typedef std::unique_ptr<IRoutableFactory> UP;
        typedef std::shared_ptr<IRoutableFactory> SP;

        bool encode(const mbus::Routable &obj, vespalib::GrowableByteBuffer &out) const override;
        mbus::Routable::UP decode(document::ByteBuffer &in, const LoadTypeSet& loadTypes) const override;
    };

    class DocumentIgnoredReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };

    /**
     * Implements a helper class to do feed message factories.
     */
    class FeedMessageFactory : public DocumentMessageFactory {
    protected:
        void myDecode(FeedMessage &msg, document::ByteBuffer &buf) const;
        void myEncode(const FeedMessage &msg, vespalib::GrowableByteBuffer &buf) const;
    };

    /**
     * Implements a helper class to do feed reply factories.
     */
    class FeedReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
        virtual uint32_t getType() const = 0;
    };

    ////////////////////////////////////////////////////////////////////////////////
    //
    // Factories
    //
    ////////////////////////////////////////////////////////////////////////////////

    class CreateVisitorMessageFactory : public DocumentMessageFactory {
        const document::DocumentTypeRepo &_repo;
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        CreateVisitorMessageFactory(const document::DocumentTypeRepo &r) : _repo(r) {}
    };

    class GetDocumentMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };

    ///////////////////////////////////////////////////////////////////////////
    //
    // Utilities
    //
    ///////////////////////////////////////////////////////////////////////////
 protected:
    /**
     * This is a complement for the vespalib::GrowableByteBuffer.putString() method.
     *
     * @param in The byte buffer to read from.
     * @return The decoded string.
     */
    static string decodeString(document::ByteBuffer &in)
    { return RoutableFactories50::decodeString(in); }

    /**
     * This is a complement for the vespalib::GrowableByteBuffer.putBoolean() method.
     *
     * @param in The byte buffer to read from.
     * @return The decoded bool.
     */
    static bool decodeBoolean(document::ByteBuffer &in)
    { return RoutableFactories50::decodeBoolean(in); }

    /**
     * Convenience method to decode a 32-bit int from the given byte buffer.
     *
     * @param in The byte buffer to read from.
     * @return The decoded int.
     */
    static int32_t decodeInt(document::ByteBuffer &in)
    { return RoutableFactories50::decodeInt(in); }

    /**
     * Convenience method to decode a 64-bit int from the given byte buffer.
     *
     * @param in The byte buffer to read from.
     * @return The decoded int.
     */
    static int64_t decodeLong(document::ByteBuffer &in)
    { return RoutableFactories50::decodeLong(in); }


    /**
     * Convenience method to decode a document id from the given byte buffer.
     *
     * @param in The byte buffer to read from.
     * @return The decoded document id.
     */
    static document::DocumentId decodeDocumentId(document::ByteBuffer &in)
    { return RoutableFactories50::decodeDocumentId(in); }

    /**
     * Convenience method to encode a document id to the given byte buffer.
     *
     * @param id  The document id to encode.
     * @param out The byte buffer to write to.
     */
    static void encodeDocumentId(const document::DocumentId &id,
                                 vespalib::GrowableByteBuffer &out)
    { return RoutableFactories50::encodeDocumentId(id, out); }
};

}

